// pages/editor/editor.js
const util = require('../../utils/util.js')
import Bmob from '../../utils/dist/Bmob-1.7.0.min.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    this.setData({
      optionsid:options.id
    })
    if(options.id=='mymenu'){   //来自我保存的菜单
      console.log(1)
      let current = Bmob.User.current();
      const query = Bmob.Query("savedata");
      query.equalTo("who", "==", current.objectId);
      query.find().then(res => {
        console.log(res)
        this.setData({
          ztsj: res
        })
      });
    }
    if(options.id=='recommended'){    //来自我推荐的菜单
      console.log(2)
      let current = Bmob.User.current();
      const query = Bmob.Query("recommendeddata");
      query.equalTo("who", "==", current.objectId);
      query.find().then(res => {
        console.log(res)
        this.setData({
          ztsj: res
        })
      });
    }

  },

  deletedata: function(e){   //删除菜单
    var id = e.currentTarget.dataset['id'];
    console.log(id)
    this.setData({
      dataid:id
    })
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确定删除？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.showToast({
            title: '删除成功',
            duration: 2000
          })
         that.ok()
        } else if (res.cancel) {
          console.log('用户点击取消')

        }
      }
    })
   //console.log(this.data.dataid,"5")
  },

  ok: function(){   //删除菜单
    
    //console.log(this.data.dataid)
   if(this.data.optionsid=='mymenu'){
     //console.log(7)
     const query = Bmob.Query('savedata');
     query.destroy(this.data.dataid).then(res => {
       console.log(res)
       let current = Bmob.User.current();   //删除后再加载一次保存的菜单
       
       const query = Bmob.Query("savedata");
       query.equalTo("who", "==", current.objectId);
       query.find().then(res => {
         console.log(res)
         this.setData({
           ztsj: res
         })
         const query = Bmob.Query('_User')     //删除后在用户表里的保存数量减一
         query.get(current.objectId).then(res => {
           res.increment('savenumber', -1)
           res.save()
         }).catch(err => {
           console.log(err)
         })
       });

     }).catch(err => {
       console.log(err)
     })
   }
    if (this.data.optionsid =='recommended'){
      //console.log(8)
      const query = Bmob.Query('recommendeddata');
      query.destroy(this.data.dataid).then(res => {
        console.log(res)

        let current = Bmob.User.current();   //删除后再加载一次推荐的菜单
        const query = Bmob.Query("recommendeddata");
        query.equalTo("who", "==", current.objectId);
        query.find().then(res => {
          console.log(res)
          this.setData({
            ztsj: res
          })
          const query = Bmob.Query('_User')     //删除后在用户表里的保存数量减一
          query.get(current.objectId).then(res => {
            res.increment('recommendednumber', -1)
            res.save()
          }).catch(err => {
            console.log(err)
          })
        });

      }).catch(err => {
        console.log(err)
      })
    }

  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})